//
//  HLHomepageViewController.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/16.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLHomepageViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "ZLCollectionViewFlowLayout.h"
#import "HLHomepageCell.h"
#import "HLHomepageHeader.h"
#import "HLBillVC.h"
#import "HLInquireVC.h"
#import "HLAdvertiseObject.h"
#import "HLTransportTypeObject.h"
#import "HLPaymentWayObject.h"
#import "HLServiceTypeObject.h"
#import "HLPackingWayObject.h"
#import "HLAdditionalObject.h"

@interface HLHomepageViewController ()
<ZLCollectionViewFlowLayoutDelegate,UICollectionViewDelegate,
UICollectionViewDataSource>

@property(nonatomic,strong)UICollectionView* collectionViewHomepage;
@property(nonatomic,strong)NSArray* arrayFunctions;
@property(nonatomic,strong)NSArray* arrayAdvertises;
@property(nonatomic,strong)UITextField* textFieldSearch;

@end

@implementation HLHomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    
    self.arrayFunctions = @[
                            @{@"image":@"hl_home_bill",@"text":@"开单"},
                            @{@"image":@"hl_home_inquire",@"text":@"运单查询"},
                            @{@"image":@"hl_home_send_stock",@"text":@"发货库存"},
                            @{@"image":@"hl_home_send",@"text":@"装车发货"},
                            @{@"image":@"hl_home_unload",@"text":@"车到卸货"},
                            @{@"image":@"hl_home_recv_stock",@"text":@"到货库存"},
                            @{@"image":@"hl_home_sign",@"text":@"派货签收"},
                            @{@"image":@"hl_home_bill",@"text":@"代收对账"},
                            @{@"image":@"hl_home_accounts",@"text":@"交账"},
                            @{@"image":@"hl_home_track",@"text":@"车辆跟踪"},
                            @{@"image":@"hl_home_finance",@"text":@"财务管理"}];
    
    [self.view addSubview:self.collectionViewHomepage];
    [self.collectionViewHomepage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    UIView* topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        UIEdgeInsets safeArea = UIEdgeInsetsZero;
        if (@available(iOS 11.0, *)) {
            safeArea = self.view.safeAreaInsets;
        }
        make.top.mas_equalTo(self.collectionViewHomepage.mas_top).offset(safeArea.top + 40);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
    }];
    
    UIButton* btnScan = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnScan setBackgroundImage:[UIImage imageNamed:@"hl_home_scan"] forState:UIControlStateNormal];
    [topView addSubview:btnScan];
    [btnScan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(topView);
        make.right.mas_equalTo(-10);
        make.size.mas_equalTo(20);
    }];
    
    [topView addSubview:self.textFieldSearch];
    [self.textFieldSearch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.mas_equalTo(topView);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(btnScan.mas_left).offset(-10);
    }];
    
    [self getAdvertise];
    [self getAllPlist];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)getAdvertise {
    [HLWebManager getAdvertise:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            self.arrayAdvertises = [HLAdvertiseObject mj_objectArrayWithKeyValuesArray:response[@"imgInfo"]];
            [self.collectionViewHomepage reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    }];
}

- (void)getAllPlist {
    [HLWebManager getPackingList:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            [[HLSingleton share]savePackingWayPlist:[HLPackingWayObject mj_objectArrayWithKeyValuesArray:response[@"defaultPackingList"]]];
        }
    } failureBlock:^(NSError *error) {
        
    }];
    
    [HLWebManager getPaymentWayList:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            [[HLSingleton share]savePaymentWayPlist:[HLPaymentWayObject mj_objectArrayWithKeyValuesArray:response[@"paymentWayList"]]];
        }
    } failureBlock:^(NSError *error) {
        
    }];
    
    [HLWebManager getServiceList:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            [[HLSingleton share]saveServicePlist:[HLServiceTypeObject mj_objectArrayWithKeyValuesArray:response[@"serviceList"]]];
        }
    } failureBlock:^(NSError *error) {
        
    }];
    
    [HLWebManager getTransportTypeList:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            [[HLSingleton share]saveTransportTypePlist:[HLTransportTypeObject mj_objectArrayWithKeyValuesArray:response[@"transportTypeList"]]];
        }
    } failureBlock:^(NSError *error) {
        
    }];
    
    [HLWebManager getAdditionalList:^(id response) {
        if ([response[@"code"]integerValue]==1) {
            [[HLSingleton share]saveAdditionalPlist:[HLAdditionalObject mj_objectArrayWithKeyValuesArray:response[@"defaultAdditionalList"]]];
        }
    } failureBlock:^(NSError *error) {
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;//self.arrayFunctions.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLHomepageCell* cell = [HLHomepageCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    NSDictionary* dic = self.arrayFunctions[indexPath.item];
    cell.imgVBusiness.image = [UIImage imageNamed:dic[@"image"]];
    cell.labelBusiness.text = dic[@"text"];
//    if (indexPath.row%3==2) {
//        NSLog(@"%zd",indexPath.row);
//        //[cell.contentView setBorderWithView:kAddLineBottom borderColor:[UIColor blackColor] borderWidth:1];
//    } else {
        [cell.contentView setBorderWithView:kAddLineBottom|kAddLineRight|kAddLineLeft borderColor:XMColor(130, 130, 130) borderWidth:1];
//    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.item) {
        case 0: {
            HLBillVC* vc = [[HLBillVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1: {
            HLInquireVC* vc = [[HLInquireVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default: {
            [SVProgressHUD showInfoWithStatus:@"等待更新"];
        }
            break;
    }
}

- (ZLLayoutType)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout *)collectionViewLayout typeOfLayout:(NSInteger)section {
    return ClosedLayout;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(ZLCollectionViewFlowLayout*)collectionViewLayout columnCountOfSection:(NSInteger)section {
    return 3;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(collectionView.frame.size.width, 250);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString : UICollectionElementKindSectionHeader]){
        HLHomepageHeader* headerView = [HLHomepageHeader headerViewWithCollectionView:collectionView forIndexPath:indexPath];
        headerView.labelHeader.text = @"主业务功能";
        [headerView reloadAdvertise:self.arrayAdvertises];
        return headerView;
    }
    return nil;
}

- (UITextField*)textFieldSearch {
    if (!_textFieldSearch) {
        _textFieldSearch = [UITextField new];
        _textFieldSearch.placeholder = @"运单号/发货人/收货人/姓名";
        _textFieldSearch.textColor = MainColor;
        UIImageView* imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_home_search"]];
        imgV.frame = CGRectMake(0, 0, 40, 20);
        imgV.contentMode = UIViewContentModeCenter;
        _textFieldSearch.rightView = imgV;
        _textFieldSearch.rightViewMode = UITextFieldViewModeAlways;
    }
    return _textFieldSearch;
}

- (UICollectionView*)collectionViewHomepage {
    if (!_collectionViewHomepage) {
        ZLCollectionViewFlowLayout *flowLayout = [[ZLCollectionViewFlowLayout alloc] init];
        flowLayout.delegate = self;
        flowLayout.itemSize = CGSizeMake(100, 100);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        _collectionViewHomepage = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionViewHomepage.dataSource = self;
        _collectionViewHomepage.delegate = self;
        _collectionViewHomepage.backgroundColor = [UIColor whiteColor];
        [_collectionViewHomepage registerClass:[HLHomepageCell class] forCellWithReuseIdentifier:[HLHomepageCell cellIdentifier]];
        [_collectionViewHomepage registerClass:[HLHomepageHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[HLHomepageHeader headerViewIdentifier]];
        
        if (@available(iOS 11.0, *)) {
            _collectionViewHomepage.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionViewHomepage;
}

@end
